package com.nf.health.app.adapter;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.nf.health.app.R;
import com.nf.health.app.datamain.activity.BloodPressureActivity;
import com.nf.health.app.datamain.activity.BloodSugarDataActivity;
import com.nf.health.app.bodyfat.BodyFatDataNewActivity;
import com.nf.health.app.datamain.activity.TemperatureDataActivity;
import com.nf.health.app.datamain.activity.UrineTestActivity;
import com.nf.health.app.models.DeviceData;
import com.nf.health.app.models.HealthStateInfo;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.Util;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Jason on 2016/3/31.
 */
public class ItemExceptionDataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private List<HealthStateInfo.ExceptionValue> exceptionValues;
    Activity act;
    public ItemExceptionDataAdapter(List<HealthStateInfo.ExceptionValue> exceptionValues,Activity act) {
        this.exceptionValues = exceptionValues;
        this.act = act;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_healthy_state_child2, parent, false);
        return new ItemExceptionViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ItemExceptionViewHolder itemHolder = (ItemExceptionViewHolder)holder;
        itemHolder.tvName.setText(exceptionValues.get(position).getName());
        itemHolder.tvValue.setText(exceptionValues.get(position).getValue());
        itemHolder.tvStatus.setText(exceptionValues.get(position).getStatus());


        itemHolder.btnMeasure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!Util.checkBluetoothAvailable()){
                    Toast.makeText(act, "不支持蓝牙4.3", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (exceptionValues.get(position).getName().contains("葡萄糖") || exceptionValues.get(position).getName().contains("胆红素") || exceptionValues.get(position).getName().contains("胴体") ||
                        exceptionValues.get(position).getName().contains("比重") || exceptionValues.get(position).getName().contains("红细胞") || exceptionValues.get(position).getName().contains("PH") ||
                        exceptionValues.get(position).getName().contains("尿蛋白") || exceptionValues.get(position).getName().contains("尿胆原") || exceptionValues.get(position).getName().contains("亚硝酸盐") ||
                        exceptionValues.get(position).getName().contains("白细胞")) {
                    Bundle bundle = new Bundle();
                    bundle.putString("deviceType", DeviceData.Type.URINE);
                    ActivityUtils.showActivity(act, UrineTestActivity.class,bundle);

                } else if (exceptionValues.get(position).getName().contains("糖")) {
                    ActivityUtils.showActivity(act, BloodSugarDataActivity.class);
                } else if (exceptionValues.get(position).getName().contains("低压") || exceptionValues.get(position).getName().contains("高压") || exceptionValues.get(position).getName().contains("高血压") ||
                        exceptionValues.get(position).getName().contains("低血压压") || exceptionValues.get(position).getName().contains("心率")) {
                    Bundle bundle = new Bundle();
                    bundle.putString("deviceType", DeviceData.Type.BLOOD_PRESSURE);
                    ActivityUtils.showActivity(act, BloodPressureActivity.class,bundle);
                } else if (exceptionValues.get(position).getName().contains("脂肪率") || exceptionValues.get(position).getName().contains("骨骼重量") || exceptionValues.get(position).getName().contains("肌肉率")
                        || exceptionValues.get(position).getName().contains("皮下脂肪") || exceptionValues.get(position).getName().contains("水含量") || exceptionValues.get(position).getName().contains("基础代谢")
                        || exceptionValues.get(position).getName().contains("BMI")
                        ) {
                    Bundle bundle = new Bundle();
                    bundle.putString("deviceType", DeviceData.Type.BALANCE);
                    ActivityUtils.showActivity(act, BodyFatDataNewActivity.class,bundle);
                } else if (exceptionValues.get(position).getValue().contains("葡萄糖") || exceptionValues.get(position).getValue().contains("胆红素") || exceptionValues.get(position).getValue().contains("胴体") ||
                        exceptionValues.get(position).getValue().contains("比重") || exceptionValues.get(position).getValue().contains("红细胞") || exceptionValues.get(position).getValue().contains("PH") ||
                        exceptionValues.get(position).getValue().contains("尿蛋白") || exceptionValues.get(position).getValue().contains("尿胆原") || exceptionValues.get(position).getValue().contains("亚硝酸盐") ||
                        exceptionValues.get(position).getValue().contains("白细胞")) {
                    Bundle bundle = new Bundle();
                    bundle.putString("deviceType", DeviceData.Type.URINE);
                    ActivityUtils.showActivity(act, UrineTestActivity.class,bundle);

                } else if (exceptionValues.get(position).getValue().contains("糖")) {
                    ActivityUtils.showActivity(act, BloodSugarDataActivity.class);
                }else if (exceptionValues.get(position).getValue().contains("低压") || exceptionValues.get(position).getValue().contains("高压") || exceptionValues.get(position).getValue().contains("高血压") ||
                        exceptionValues.get(position).getValue().contains("低血压压") || exceptionValues.get(position).getValue().contains("心率")) {
                    Bundle bundle = new Bundle();
                    bundle.putString("deviceType", DeviceData.Type.BLOOD_PRESSURE);
                    ActivityUtils.showActivity(act, BloodPressureActivity.class,bundle);
                } else if (exceptionValues.get(position).getValue().contains("脂肪率") || exceptionValues.get(position).getValue().contains("骨骼重量") || exceptionValues.get(position).getValue().contains("肌肉率")
                        || exceptionValues.get(position).getValue().contains("皮下脂肪") || exceptionValues.get(position).getValue().contains("水含量") || exceptionValues.get(position).getValue().contains("基础代谢")
                        || exceptionValues.get(position).getValue().contains("胖")|| exceptionValues.get(position).getValue().contains("瘦")) {
                    Bundle bundle = new Bundle();
                    bundle.putString("deviceType", DeviceData.Type.BALANCE);
                    ActivityUtils.showActivity(act, BodyFatDataNewActivity.class,bundle);
                } else if (exceptionValues.get(position).getName().contains("体温")) {
                    Bundle bundle = new Bundle();
                    bundle.putString("deviceType", DeviceData.Type.TEMPERATURE);
                    ActivityUtils.showActivity(act, TemperatureDataActivity.class,bundle);
                }else if (exceptionValues.get(position).getValue().contains("温")) {
                    Bundle bundle = new Bundle();
                    bundle.putString("deviceType", DeviceData.Type.TEMPERATURE);
                    ActivityUtils.showActivity(act, TemperatureDataActivity.class,bundle);
                }
            }

        });

    }

    @Override
    public int getItemCount() {
        return exceptionValues.size();
    }

    class ItemExceptionViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_value)
        TextView tvValue;
        @Bind(R.id.tv_status)
        TextView tvStatus;
        @Bind(R.id.btn_measure)
        TextView btnMeasure;
        public ItemExceptionViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }
}
